import Message from "@/websocket/message";
import protoRoot from "@/proto/proto";
import {command} from "@/websocket/ws_const";
import Vue from 'vue'
import store from "@/store"

//缓存proto的类型
const protoMap = new Map();

//获取消息序号，用于聊天
const getMsgId = (cb) => {
    let reqData = {
        timestamp: new Date().getTime()
    };
    console.log("获取消息序号请求：", JSON.stringify(reqData));
    let reqBuf = createReqBuf(command.GENERATE_ID.key, 0n, "im.Heartbeat", reqData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf, cb);
};

//发送单聊消息
const sendP2PMsg = (msgId, msgData, cb) => {
    console.log("发送单聊消息请求：", JSON.stringify(msgData));
    let reqBuf = createReqBuf(command.P2P_SENDER.key, BigInt(msgId.toString()), "im.P2PRequest", msgData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf, cb);
};

//单聊发送给对方ack消息
const sendP2PAckMsg = (msgId, msgData) => {
    console.log("发送单聊ack消息请求：", JSON.stringify(msgData));
    let reqBuf = createReqBuf(command.P2P_NOTIFY_ACK.key, msgId, "im.P2PRequest", msgData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf);
};

//拉取离线消息
const pullOfflineMsg = (targetType, lastTimestamp = 0, pageSize = 30) => {
    let pullReqData = {
        targetType: targetType,
        pageSize: pageSize,
        lastTimestamp: lastTimestamp
    };
    console.log("离线消息拉取请求：", JSON.stringify(pullReqData));
    let reqBuf = createReqBuf(command.OFFLINE_MSG_PULL.key, 0n, "im.OfflineMsgRequest", pullReqData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf);
};

//离线消息拉取确认
const offlineAckMsg = (targetType, timestamp) => {
    let reqData = {
        targetType: targetType,
        lastTimestamp: timestamp
    };
    let reqBuf = createReqBuf(command.OFFLINE_MSG_ACK.key, 0n, "im.OfflineMsgAckRequest", reqData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf);
};

//发送心跳
const sendHeartbeat = () => {
    const heartbeatData = {
        timestamp: new Date().getTime()
    };
    console.log("发送心跳请求：", JSON.stringify(heartbeatData));
    let reqBuf = createReqBuf(command.HEART_BEAT.key, 0n, "im.Heartbeat", heartbeatData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf);
};

//登录请求
const login = () => {
    let loginReqData = {
        username: store.state.user.vuex_token,
        password: "",
        deviceNumber: store.state.user.vuex_device_number
    };
    console.log("发送登录请求：", JSON.stringify(loginReqData));
    let reqBuf = createReqBuf(command.AUTH.key, 0n, "im.AuthRequest", loginReqData);
    Vue.prototype.$ws.sendSocketMessage(reqBuf);
};

/**
 * 获取proto类型
 * @author miaoguoxin
 * @date 2021/4/21
 */
const getProto = (protoName) => {
    let proto = protoMap.get(protoName);
    if (typeof proto == "undefined") {
        proto = protoRoot.lookup(protoName);
        protoMap.set(protoName, proto);
    }
    return proto;
};

/**
 * 创建请求数据buf
 * @author miaoguoxin
 * @date 2021/3/31
 */
const createReqBuf = (commandType, msgId, protoName, reqData) => {
    let message = new Message();
    let proto = getProto(protoName);
    let bytes = proto.encode(proto.create(reqData)).finish();
    message.encodeRequest(commandType, msgId, bytes);
    return message.buf;
};

export default {
    getMsgId,
    sendP2PMsg,
    sendP2PAckMsg,
    pullOfflineMsg,
    sendHeartbeat,
    login,
    offlineAckMsg,
    getProto
}
